// pages/projectDetail/projectDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    projectData: {},
    thirdButtonText: '',
    terminatedBtnText: '',
    showTerminatedBtn: false,
    targetId: '',
    waitId: '',
  },
  suspendProject: function() {
    // 暂停/启动项目
    let that = this
    let config = wx.getStorageSync('config')
    let t = that.data.projectData.projectStatus == 2 ? '启动' : '暂停'
    wx.showModal({
      content: '确定' + t + '该项目吗？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: config.host + 'api/project/updateProjectStatus',
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              projectId: that.data.projectData.projectId,
              status: that.data.projectData.projectStatus == 2 ? 0 : 2
            },
            success(res) {
              if (res.data.status == 200) {
                wx.showToast({
                  title: t + '项目成功',
                  icon: 'none'
                })
              }
              // 刷新页面数据
              that.getProjectData(that.data.projectData.projectId)
            }
          })
        } else if (res.cancel) {
          console.log('取消暂停')
        }
      }
    })
  },
  endProject: function() {
    // 终止项目
    let that = this
    let config = wx.getStorageSync('config')
    wx.showModal({
      content: '是否终止该项目',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: config.host + 'api/project/saveProjectReview',
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              projectId: that.data.projectData.projectId,
              principalId: that.data.projectData.principalId,
              type: 3
            },
            success(res) {
              if (res.data.status == 200) {
                wx.showToast({
                  title: '终止项目申请成功，等待上级审批',
                  icon: 'none'
                })
                // 刷新页面数据
                that.getProjectData(that.data.projectData.projectId)
              }
            }
          })
        } else if (res.cancel) {
          console.log('取消终止')
        }
      }
    })
  },
  modifyProject: function(e) {
    // 修改项目
    let projectId = e.currentTarget.dataset.projectid
    wx.redirectTo({
      url: '/pages/modifyProject/modifyProject?projectId=' + projectId,
    })
  },
  getProjectData: function(projectId) {
    // 获取项目详情
    let that = this
    let config = wx.getStorageSync('config')
    wx.request({
      url: config.host + 'api/project/projectInfo',
      method: 'GET',
      data: {
        projectId: projectId
      },
      success(res) {
        if (res.data.status == 200) {
          that.setData({
            projectData: res.data.data
          })
          if (res.data.data.projectStatus == 0) {
            that.setData({
              thirdButtonText: '暂停'
            })
          } else if (res.data.data.projectStatus == 1) {
            that.setData({
              showTerminatedBtn: true,
              terminatedBtnText: '项目已过期'
            })
          } else if (res.data.data.projectStatus == 2) {
            that.setData({
              thirdButtonText: '启动'
            })
          } else if (res.data.data.projectStatus == 3) {
            that.setData({
              showTerminatedBtn: true,
              terminatedBtnText: '项目已终止'
            })
          } else if (res.data.data.projectStatus == 4) {
            that.setData({
              thirdButtonText: '暂停'
            })
          }
        }
      }
    })
  },
  agreeApp: function() {
    // 同意审批
    let that = this
    let config = wx.getStorageSync('config')
    wx.request({
      url: config.host + 'api/project/updateProjectReview',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        reviewId: that.data.waitId,
        status: 1
      },
      success(res) {
        if (res.data.status == 200) {
          wx.showToast({
            title: '已同意审批',
            icon: 'none'
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 800)
        }
      }
    })
  },
  refuseApp: function() {
    // 拒绝审批
    let that = this
    let config = wx.getStorageSync('config')
    wx.request({
      url: config.host + 'api/project/updateProjectReview',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        reviewId: that.data.waitId,
        status: 2
      },
      success(res) {
        if (res.data.status == 200) {
          wx.showToast({
            title: '已拒绝审批',
            icon: 'none'
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 800)
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //获取项目id
    let projectId = options.projectId
    let targetId = options.targetId
    let waitId = options.waitId
    this.getProjectData(projectId)
    wx.setStorage({
      key: 'baseProjectId',
      data: projectId,
    })
    this.setData({
      targetId: targetId == 'undefined' ? '' : targetId,
      waitId: waitId == 'undefined' ? '' : waitId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // let projectId = wx.getStorageSync('baseProjectId')
    // this.getProjectData(projectId)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})